/**

Author： EmperorHeart

UpdateDate: 2023-07-11

Desc: 增加if、in条件的使用


*/
capture program drop granger2docx
program granger2docx
version 16.0

syntax varlist [if] [in] using/ [,replace title(string) fmt(string) halign(string) width1(string) width2(string) language(string) lags(string) stats(string) style(string)]

//创意设计：中英文切换、卡方统计值和F统计值切换、

//设置默认值
qui{
	if "`title'"==""{
		local title = "表4 格兰杰因果检验"
	}
	if "`fmt'"==""{
		local fmt="%6.4f"
	}
	if "`halign'"==""{
		local halign="center"
	}
	if "`width1'"==""{
		local width1="1.5pt"
	}
	if "`width2'" == ""{
		local width2="0.75pt"
	}
	if "`language'"==""{
		local language="Chinese"
	}
	if "`lags'"==""{
		local lags="2"
	}
	if "`stats'"==""{
		local stats="F"
	}
	if "`style'"==""{
		local style="style1"
	}
	

}


*变量计数器
local num=0 
foreach v of local varlist{
	local num =`num' + 1
}
*********************************第一种 F/Chi2风格的统计表 ***********************
if "`style'"=="style1"{
	local chang=`num'*(`num'-1)

	*装数矩阵：第一列为F统计值，第二列为P值，第三列为卡方统计值，第4列为P值
	mat juzhen=J(`chang',4,.)

	local num_1 = `num' - 1
	local jishuqi=0

	tokenize `varlist'
	qui{
	forv i=1/`num_1'{

		local start=`i'+1

		forv j=`start'/`num'{

		 local jishuqi=`jishuqi'+1
		 gcause ``i'' ``j'' `in' `if',lags("`lags'")
			  mat juzhen[`jishuqi',1]=r(F)
			  mat juzhen[`jishuqi',2]==r(p)
			  mat juzhen[`jishuqi',3]==r(F_a)
			  mat juzhen[`jishuqi',4]==r(p_a)
			  
		 local jishuqi=`jishuqi'+1
		 gcause  ``j''  ``i'' `in' `if',lags("`lags'")
			  mat juzhen[`jishuqi',1]=r(F)
			  mat juzhen[`jishuqi',2]==r(p)
			  mat juzhen[`jishuqi',3]==r(F_a)
			  mat juzhen[`jishuqi',4]==r(p_a)

	}

	}

	}
	* 统计量判断
		if "`stats'"=="F"{
		mat cc=juzhen[.,1..2]
		}

		if "`stats'"=="Chi2"{
		mat cc=juzhen[.,3..4]
		}

	* 检验假设的结果
	local Result = ""
	forvalues i=1/`chang'{
		local p=cc[`i',2]
		
		if `p'<0.1{
		local Result = "`Result'" + " "+"拒绝"
		} 
		else {
		local Result = "`Result'" + " "+"不能拒绝"
		}
	}

	local Result = strtrim("`Result'") 


	* 开始装表
	putdocx clear
	putdocx begin
	putdocx paragraph,spacing(after,0) halign(center) 
	putdocx text ("`title'")

	putdocx table cv=matrix(cc),nformat("`fmt'") border(all, nil) ///
	border(top,,,"`width1'") halign("`halign'") layout(autofitcontents)

	putdocx table cv(.,1),addcols(1,before)
	putdocx table cv(.,3),addcols(1,after)
	putdocx table cv(1,.),addrows(1,before)


	putdocx table cv(1,1)=("原假设")

	putdocx table cv(1,2)=("`stats'统计值")
	putdocx table cv(1,3)=("P值")
	putdocx table cv(1,4)=("结论")
	putdocx table cv(1,.),border(bottom,,,"`width2'")  //第二条线
	local end=`chang'+1
	putdocx table cv(`end',.),border(bottom,,,"`width1'")  //第三条线

	//循环遍历生成序列对
	tokenize `varlist'

	local hang=1
	forvalues i=1/`num_1'{
		local m=`i'+1
		forvalues j=`m'/`num'{
		
			local hang=`hang'+1
			if "`language'"=="Chinese"{
				putdocx table cv(`hang',1)=("``j''不是``i''的格兰杰成因")
			} 
			else{
				putdocx table cv(`hang',1)=("``j'' does not Granger-cause ``i''")
			}
			
			local hang=`hang'+1
			if "`language'"=="Chinese"{
				putdocx table cv(`hang',1)=("``i''不是``j''的格兰杰成因")
			} 
			else{
				putdocx table cv(`hang',1)=("``i'' does not Granger-cause ``j''")
			}

		}

	}




	//循环填充结论
	tokenize `Result'
	forvalues i = 1/`chang'{
		local hang=`i'+1
		putdocx table cv(`hang',4)=("``i''")
	}


	putdocx table cv(.,1),halign("`halign'")
	putdocx table cv(.,2),halign("`halign'")
	putdocx table cv(.,3),halign("`halign'")
	putdocx table cv(.,4),halign("`halign'")
}


*************************************第二种VAR风格********************************
if "`style'"=="style2"{
	qui{


		var `varlist' `in' `if',lags(1/`lags')   //基于VAR得出的Granger因果检验
		vargranger 
		mat G=r(gstats)

		tokenize `varlist'

		putdocx clear
		putdocx begin
		putdocx paragraph, spacing(after,0) halign(center)
		putdocx text ("`title'")
		putdocx table mat=mat(G),rownames colnames nformat(%6.3f) border(all,nil)

		putdocx table mat(1,.),addrows(1,before)
		putdocx table mat(1,1)=("被解释变量:"+"`1'"),colspan(4) halign(center)
		putdocx table mat(1,.),border(top,,,`width1') border(bottom,,,`width2') 
		putdocx table mat(2,1)=("解释变量")
		putdocx table mat(2,.),border(bottom,,,`width1') 

forvalues i=2/`num'{
	local pos=(`i'-1)*(`num'+2)+1
	local pos_1=`pos'+1

	putdocx table mat(`pos',.),addrows(2,before)
	putdocx table mat(`pos',1)=("被解释变量:"+"``i''"),colspan(4) halign(center)
	putdocx table mat(`pos',.),border(top,,,`width1') border(bottom,,,`width2')
	putdocx table mat(`pos_1',1)=("解释变量")
	putdocx table mat(`pos_1',2)=("chi2")
	putdocx table mat(`pos_1',3)=("df")
	putdocx table mat(`pos_1',4)=("P")
	putdocx table mat(`pos_1',.),border(bottom,,,`width1')
}
	local end=`num'*(`num'+2)
	putdocx table mat(.,2),nformat(`fmt')
	putdocx table mat(.,3),nformat(%6.0f)
	putdocx table mat(.,4),nformat(`fmt')
	putdocx table mat(`end',.),border(bottom,,,`width1')
	}
}



putdocx save `"`using'"',`replace'
di as txt `"Granger-cause Test have been written to file {browse "`using'"}"'

end


